/**
 * 发布订阅模式
 * 将事件 [ click:[ fn,fn2,fn3... ],click2:[fn1,fn2...]  ]
 */
class EventEmitter {
	constructor() {
		this.sub = {}
	}
	on(eventName, fn) {
		this.sub[eventName] = this.sub[eventName] || []
		this.sub[eventName].push(fn)
	}
	emit(eventName) {
		this.sub[eventName] && this.sub[eventName].forEach((fn) => fn())
	}
	off(eventName, fn) {
		this.sub[eventName] &&
			(this.sub[eventName] = this.sub[eventName].filter((item) => item != fn))
	}
}

const ev = new EventEmitter()
function say() {
	console.log('say')
}
function say2() {
	console.log('say2')
}
ev.on('click', say)
ev.on('click', say2)

// ev.on('click2', say2)
ev.emit('click')
ev.off('click', say2)
ev.emit('click')
